package nobu.nakata.nfcfare.resource.impl;

import java.util.Calendar;
import java.util.Locale;

import net.sf.jsr107cache.Cache;
import nobu.nakata.nfcfare.resource.ISuicaUsageResource;
import nobu.nakata.nfcfare.resource.item.ISuicaUsageResourceItem;
import nobu.nakata.nfcfare.resource.mapper.SuicaUsageResourceMapper;
import nobu.nakata.nfcfare.resource.util.ServiceResourceUtil;

import org.restlet.data.Status;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;


public class SuicaUsageResource extends ServerResource implements ISuicaUsageResource {
	public static String USAGE_DATA;
	public static String trainBaseUrl;
	public static String busBaseUrl;
	
	/* (non-Javadoc)
	 * @see nobu.nakata.nfcfare.resource.ISuicaUsageResource#retreive()
	 */
	@Override
	public ISuicaUsageResourceItem retreive() {
		try {
			Cache cache = ServiceResourceUtil.makeCache();
			SuicaUsageResourceMapper mapper = new SuicaUsageResourceMapper(
					cache, trainBaseUrl, busBaseUrl, Calendar.getInstance(Locale.JAPAN));
			return mapper.makeByUsageData(getUsageData());
		} catch(ResourceException e) {
			throw e;
		} catch(Throwable e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST);
		} finally {
		}
	}
	
	
	private String getUsageData() {
		return (String) getRequestAttributes().get(USAGE_DATA);
	}
}
